package com.yq.ylmall.user.controller;


import com.yq.ylmall.dto.user.UserInfoDto;
import com.yq.ylmall.model.user.UserAddress;
import com.yq.ylmall.model.user.UserInfo;
import com.yq.ylmall.result.Result;
import com.yq.ylmall.result.ResultCode;
import com.yq.ylmall.user.service.IUserAddressService;
import com.yq.ylmall.user.service.IUserInfoService;
import com.yq.ylmall.vo.user.UserDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author cherry
 * @since 2024-06-03
 */
@Api(tags = "用户管理控制器")
@RestController
@RequestMapping("/admin/user")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserAddressService userAddressService;


    @ApiOperation(value = "分页查询用户信息")
    @PostMapping("/page")
    public Result selectUserInfoPage(@RequestBody UserInfoDto userInfoDto){
        System.out.println("Received UserInfoDto: " + userInfoDto);
        System.out.println("Page number: " + userInfoDto.getPageNum());
        System.out.println("Page size: " + userInfoDto.getPageSize());
        return Result.ok(userInfoService.queryUserInfoByPage(userInfoDto));
    }

    @ApiOperation(value = "查看用户详情")
    @GetMapping("/findUserDetailByUserId/{id}")
    public Result findUserDetailByUserId(@PathVariable Long id){
        //通过id找到userInfo
        UserInfo userInfo = userInfoService.getById(id);
        //通过userid找到userAddress
        List<UserAddress> addressList = userAddressService.getAddressByUserId(id);

        //创建一个用户详情VO
        UserDetailVo userDetailVo = new UserDetailVo();
        userDetailVo.setUserInfo(userInfo);

        //遍历查到的地址集合,将内容添加进用户详情vo
        for (UserAddress address : addressList) {
            userDetailVo.getUserAddressList().add(address);
        }
        return Result.ok(userDetailVo);
    }

    //停用or启用用户
    @ApiOperation(value = "停用或启用用户")
    @PutMapping("/makeUserStatusStopById/{userId}/{statusTag}")
    public Result makeUserStatusStopById(@PathVariable Long userId,
                                         @PathVariable Integer statusTag){
        Boolean isOk = userInfoService.stopUserStatusById(userId,statusTag);
        if (isOk){
            return Result.ok();
        }else {
            return Result.fail(ResultCode.DATA_ERROR);
        }
    }
    

}
